<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Admin\Form;

use Admin\Data\Common;
use Admin\Data\Config;
use Admin\Filter\ImageRegenerate;
use Admin\Validator\ImageSuffixValidator;
use Laminas\Form\Form;
use Laminas\I18n\Translator\Translator;

class CustomerSetForm extends Form
{
    private $translator;
    public $inputFilter;

    public function __construct($name = 'customer-set-form', $options = [])
    {
        parent::__construct($name, $options);
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');

        $this->translator = new Translator();

        $this->inputFilter = $this->addInputFilter();

        $this->addElements();
        $this->addInputFilter();
    }

    public function addElements()
    {
        /*=============================基本设置=============================*/
        $this->add([
            'type'  => 'radio',
            'name'  => 'register_status',
            'options' => [
                'label_attributes' => [
                    'class' => 'radio-inline'
                ],
                'disable_inarray_validator' => true
            ],
            'attributes'    => [
                'value'         => 1
            ]
        ]);

        $this->add([
            'type'  => 'textarea',
            'name'  => 'register_close_message',
            'attributes'    => [
                'id'    => 'register_close_message',
                'class' => 'form-control',
                'rows'  => 4
            ]
        ]);

        $this->add([
            'type'  => 'file',
            'name'  => 'default_user_avatar',
            'attributes'    => [
                'id'            => 'default_user_avatar',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'select',
            'name'  => 'default_user_group_id',
            'attributes'    => [
                'id'            => 'default_user_group_id',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'select',
            'name'  => 'user_register_audit',
            'attributes'    => [
                'id'            => 'user_register_audit',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'textarea',
            'name'  => 'user_register_retain',
            'attributes'    => [
                'id'    => 'user_register_retain',
                'class' => 'form-control',
                'rows'  => 4
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'address_image1',
            'attributes'    => [
                'id'            => 'address_image1',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'address_image2',
            'attributes'    => [
                'id'            => 'address_image2',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'login_account_edit',
            'attributes'    => [
                'id'            => 'login_account_edit',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'number',
            'name'  => 'account_edit_num',
            'attributes'    => [
                'id'    => 'account_edit_num',
                'class' => 'form-control',
                'min'   => 1,
                'value' => 1
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'user_register_url',
            'attributes'    => [
                'id'            => 'user_register_url',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'user_login_url',
            'attributes'    => [
                'id'            => 'user_login_url',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'forget_password_url',
            'attributes'    => [
                'id'            => 'forget_password_url',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'register_welcome_email',
            'attributes'    => [
                'id'            => 'register_welcome_email',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'welcome_email_title',
            'attributes'    => [
                'id'            => 'welcome_email_title',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'textarea',
            'name'  => 'welcome_email_body',
            'attributes'    => [
                'id'    => 'welcome_email_body',
                'class' => 'form-control',
                'rows'  => 5
            ]
        ]);

        /*=============================客户协议=============================*/
        $this->add([
            'type'  => 'textarea',
            'name'  => 'register_agreement',
            'attributes'    => [
                'id'    => 'register_agreement',
                'class' => 'form-control',
                'rows'  => 6
            ]
        ]);

        /*=============================登录注册=============================*/
        $this->add([
            'type'  => 'multiCheckbox',
            'name'  => 'quick_register',
            'options' => [
                'label_attributes' => [
                    'class' => 'checkbox-inline'
                ]
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'register_email_state',
            'attributes'    => [
                'id'            => 'register_email_state'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'register_phone_state',
            'attributes'    => [
                'id'            => 'register_phone_state'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'register_complex_passwd_state',
            'attributes'    => [
                'id'            => 'register_complex_passwd_state'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'register_phone_captcha',
            'attributes'    => [
                'id'            => 'register_phone_captcha'
            ]
        ]);

        $this->add([
            'type'  => 'multiCheckbox',
            'name'  => 'quick_login',
            'options' => [
                'label_attributes' => [
                    'class' => 'checkbox-inline'
                ]
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'quick_login_add_user',
            'attributes'    => [
                'id'            => 'quick_login_add_user',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'multiCheckbox',
            'name'  => 'login_item',
            'options' => [
                'label_attributes' => [
                    'class' => 'checkbox-inline'
                ]
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'login_complex_passwd_state',
            'attributes'    => [
                'id'            => 'login_complex_passwd_state'
            ]
        ]);

        /*=============================第三方登录=============================*/
        $this->add([
            'type'  => 'checkbox',
            'name'  => 'qq_login_state',
            'attributes'    => [
                'id'            => 'qq_login_state',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'qq_app_id',
            'attributes'    => [
                'id'            => 'qq_app_id',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'qq_app_key',
            'attributes'    => [
                'id'            => 'qq_app_key',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'weixin_login_state',
            'attributes'    => [
                'id'            => 'weixin_login_state',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'weixin_app_id',
            'attributes'    => [
                'id'            => 'weixin_app_id',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'weixin_app_key',
            'attributes'    => [
                'id'            => 'weixin_app_key',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'weixin_web_login_state',
            'attributes'    => [
                'id'            => 'weixin_web_login_state',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'weixin_web_app_id',
            'attributes'    => [
                'id'            => 'weixin_web_app_id',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'weixin_web_app_key',
            'attributes'    => [
                'id'            => 'weixin_web_app_key',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'checkbox',
            'name'  => 'alipay_login_state',
            'attributes'    => [
                'id'            => 'alipay_login_state',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'text',
            'name'  => 'alipay_app_id',
            'attributes'    => [
                'id'            => 'alipay_app_id',
                'class'         => 'form-control'
            ]
        ]);

        $this->add([
            'type'  => 'textarea',
            'name'  => 'alipay_private_key',
            'attributes'    => [
                'id'    => 'alipay_private_key',
                'class' => 'form-control',
                'rows'  => 5
            ]
        ]);

        $this->add([
            'type'  => 'textarea',
            'name'  => 'alipay_public_key',
            'attributes'    => [
                'id'    => 'alipay_public_key',
                'class' => 'form-control',
                'rows'  => 5
            ]
        ]);

        /*=============================充值与提现=============================*/
        $this->add([
            'type'  => 'checkbox',
            'name'  => 'recharge_state',
            'attributes'    => [
                'id'            => 'recharge_state',
                'class'         => 'form-control'
            ]
        ]);
        $this->add([
            'type'  => 'checkbox',
            'name'  => 'withdraw_state',
            'attributes'    => [
                'id'            => 'withdraw_state',
                'class'         => 'form-control'
            ]
        ]);
        $this->add([
            'type'  => 'text',
            'name'  => 'withdraw_fee',
            'attributes'    => [
                'id'    => 'withdraw_fee',
                'class' => 'form-control',
                'value' => 0
            ]
        ]);
        /*=============================验证设置=============================*/
        /*$this->add([
            'type'  => 'select',
            'name'  => 'user_login_password_edit',
            'attributes'    => [
                'id'            => 'user_login_password_edit',
                'class'         => 'form-control'
            ]
        ]);
        $this->add([
            'type'  => 'select',
            'name'  => 'user_pay_password_edit',
            'attributes'    => [
                'id'            => 'user_pay_password_edit',
                'class'         => 'form-control'
            ]
        ]);
        $this->add([
            'type'  => 'select',
            'name'  => 'user_email_edit',
            'attributes'    => [
                'id'            => 'user_email_edit',
                'class'         => 'form-control'
            ]
        ]);
        $this->add([
            'type'  => 'select',
            'name'  => 'user_phone_edit',
            'attributes'    => [
                'id'            => 'user_phone_edit',
                'class'         => 'form-control'
            ]
        ]);*/
    }

    public function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        $inputFilter->add([
            'name'      => 'register_status',
            'required'  => true,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [1, 0]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'register_close_message',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'type'      => 'Laminas\InputFilter\FileInput',
            'name'      => 'default_user_avatar',
            'required'  => false,
            'validators'=> [
                ['name' => 'FileUploadFile'],
                ['name' => ImageSuffixValidator::class],
                [
                    'name' => 'FileMimeType',
                    'options' => [
                        'mimeType' => Common::configValue('image_type')
                    ]
                ],
                ['name' => 'FileIsImage'],
                [
                    'name' => 'FileSize',
                    'options' => Common::configValue('upload_image_max')
                ]
            ],
            'filters'  => [
                [
                    'name' => 'FileRenameUpload',
                    'options' => [
                        'target' => Config::COMMON_IMAGE_PATH . 'default_user_avatar.png',
                        'overwrite'=>true
                    ]
                ],
                ['name' => ImageRegenerate::class]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'default_user_group_id',
            'required'  => true,
            'filters'   => [
                ['name' => 'ToInt']
            ],
            'validators'=> [
                [
                    'name'      => 'GreaterThan',
                    'options'   => [
                        'min'   => 0
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'user_register_audit',
            'required'  => false
        ]);

        $inputFilter->add([
            'name'      => 'user_register_retain',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'address_image1',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'address_image2',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'login_account_edit',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'account_edit_num',
            'required'  => true,
            'filters'   => [
                ['name' => 'ToInt']
            ],
            'validators'=> [
                [
                    'name'      => 'GreaterThan',
                    'options'   => [
                        'min'   => 0
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'user_register_url',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'user_login_url',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'forget_password_url',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'register_welcome_email',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'welcome_email_title',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'welcome_email_body',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim']
            ]
        ]);

        /*=============================客户协议=============================*/
        $inputFilter->add([
            'name'      => 'register_agreement',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim']
            ]
        ]);

        /*=============================登录注册=============================*/
        $inputFilter->add([
            'name'      => 'quick_register',
            'required'  => false
        ]);

        $inputFilter->add([
            'name'      => 'register_email_state',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'register_phone_state',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'register_phone_captcha',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'register_complex_passwd_state',
            'required'  => false
        ]);

        $inputFilter->add([
            'name'      => 'quick_login',
            'required'  => false
        ]);

        $inputFilter->add([
            'name'      => 'quick_login_add_user',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'login_item',
            'required'  => false
        ]);

        $inputFilter->add([
            'name'      => 'login_complex_passwd_state',
            'required'  => false
        ]);
        /*=============================第三方登录=============================*/
        $inputFilter->add([
            'name'      => 'qq_login_state',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'qq_app_id',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'qq_app_key',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'weixin_login_state',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'weixin_app_id',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'weixin_app_key',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'weixin_web_login_state',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'weixin_web_app_id',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'weixin_web_app_key',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'alipay_login_state',
            'required'  => false,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [0, 1]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'alipay_app_id',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'alipay_private_key',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        $inputFilter->add([
            'name'      => 'alipay_public_key',
            'required'  => false,
            'filters'   => [
                ['name' => 'StringTrim'],
                ['name' => 'StripTags']
            ]
        ]);

        /*=============================充值与提现=============================*/
        $inputFilter->add([
            'name'      => 'recharge_state',
            'required'  => true,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [1, 0]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'withdraw_state',
            'required'  => true,
            'validators'=> [
                [
                    'name'      => 'InArray',
                    'options'   => [
                        'haystack'  => [1, 0]
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'withdraw_fee',
            'required'  => false,
            'filters'   => [
                ['name' => 'ToFloat']
            ],
            'validators'=> [
                [
                    'name'      => 'GreaterThan',
                    'options'   => [
                        'min'   => 0,
                        'max'   => 100,
                        'inclusive' => true,
                        'setMessage' => $this->translator->translate('提现手续费率错误')
                    ],
                ]
            ]
        ]);

        return $inputFilter;
    }
}